package ngames.com.sushitv.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.AnimeListAdapter;
import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.temp.Interface.AnimeInterface;
import ngames.com.sushitv.temp.Module.AnimeFactory;
import ngames.com.sushitv.temp.Presenter.AnimePresenter;

/**
 * Created by edmond on 16-7-26.
 */

/**
 * 下拉刷新
 * （开始）出现刷新
 * （结束）取消刷新
 * 上拉加载
 * （开始）显示正在加载
 * （结束）停止夹在{没有更多，正在加载}
 * 初始化数据
 * （开始）显示数据正在夹在
 * （结束）停止{显示数据，网络出现问题}*/
public class AnimeFragment extends Fragment
        implements AnimeInterface {
    private View view;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private AnimeListAdapter adapter;
    private AnimePresenter animePresenter;

    private RecyclerView.LayoutManager layoutManager;

    private List<Anime> animesList = new ArrayList<>();

    private boolean isInit = false;
    private boolean isLoad = false;
    private Activity activity;

    private int page = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_anime,container,false);
        activity = getActivity();

        AnimeFactory animeFactory = new AnimeFactory();
        animePresenter = new AnimePresenter((AppApplication) activity.getApplication(),animeFactory,this);
        layoutManager = new LinearLayoutManager(getActivity());

        adapter = new AnimeListAdapter(activity,animesList, (AppApplication) activity.getApplication());

        findViews();
        initData();
        initAction();

        return view;
    }

    private void findViews(){
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
    }

    private void initData(){
        animePresenter.initData();
    }

    private void initAction(){
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                int totalItemCount = layoutManager.getItemCount();
                Log.d("AnimeFragment", String.valueOf(lastVisibleItem));
                Log.d("AnimeFragment", String.valueOf(totalItemCount));
                if(lastVisibleItem>=totalItemCount-1&&!isLoad){
                    isLoad = true;
                    Log.d("AnimeFragment","LoadMore");
                    animePresenter.moreData();
                }
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                animePresenter.refeshData();
            }
        });
    }

    @Override
    public void initingData() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initedData() {
        isInit = true;
        page++;
    }

    @Override
    public void moreingData() {

    }

    @Override
    public void moredData() {
        adapter.notifyDataSetChanged();
        isLoad = false;
        page++;
    }

    @Override
    public void refreshingData() {
        Log.d("AnimeFragment","refreshing");
    }

    @Override
    public void refreshedData() {
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public List<Anime> getAnimesList() {
        return animesList;
    }

    @Override
    public int getPage() {
        return page;
    }
}
